package com.vcredit.creditcat.web.h5;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.product.h5.IAfterCreditManager;
import com.vcredit.creditcat.dto.product.h5.BindBankCardParamDto;
import com.vcredit.creditcat.dto.product.h5.CheckOrderResultDto;
import com.vcredit.creditcat.dto.product.h5.CreateOrderParamDto;
import com.vcredit.creditcat.dto.product.h5.LoanBindCardListDto;
import com.vcredit.creditcat.dto.product.h5.LoanOrderIndexDto;
import com.vcredit.creditcat.dto.product.h5.QueryBankByCardResultDto;
import com.vcredit.creditcat.dto.product.h5.QueryContractResultDto;
import com.vcredit.creditcat.dto.product.h5.QueryOrderStatusResultDto;
import com.vcredit.creditcat.dto.product.h5.SdOrderReviewInfoDto;
import com.vcredit.creditcat.dto.product.h5.SdTryCalculateParamDto;
import com.vcredit.creditcat.dto.product.h5.SdTryCalculateResultDto;
import com.vcredit.creditcat.dto.product.h5.SubmitBindCardParamDto;
import com.vcredit.creditcat.dto.product.h5.TryCalculateParamDto;
import com.vcredit.creditcat.dto.product.h5.TryCalculateResultDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.creditcat.vo.product.SignatureParam;
import com.vcredit.creditcat.web.app.BaseCreditCatController;
import com.vcredit.framework.annotation.Log;

@RestController
@RequestMapping("afterCredit")
public class AfterCreditController extends BaseCreditCatController{
	
	private static final Logger LOG = LoggerFactory.getLogger(AfterCreditController.class);
	
	private static final String ERROR_MSG = "系统异常，请稍后再试";
	
	
	
	
	@Reference(version = "1.0")
	private IAfterCreditManager afterCreditManager;
	
	/**
	 * 查询闪贷订单审核信息
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("sd/queryReviewInfo")
	public JsonResult<SdOrderReviewInfoDto> queryReviewInfo(HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			SdOrderReviewInfoDto result = afterCreditManager.queryReviewInfo(cust);
			
					
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------闪贷查询订单审核信息异常!! custId=" + cust.getCustId(),e);
		}
		return JsonResult.ErrorResponse(ERROR_MSG);
	}
	
	@Log
	@Valid
	@RequestMapping("sd/tryCalculate")
	public JsonResult<SdTryCalculateResultDto> sdTryCalculate(
			@RequestBody SdTryCalculateParamDto param,HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			SdTryCalculateResultDto result = afterCreditManager.sdTryCalculate(param,cust);
			return new JsonResult<SdTryCalculateResultDto>(result);
		} catch (Exception e) {
			LOG.error("------------闪贷试算异常!! custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	
	@Log
	@Valid
	@RequestMapping("sd/getDeviceId")
	public JsonResult sdGetDeviceId(HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);			
			String  deviceId=afterCreditManager.sdGetDeviceId(cust);			
			return JsonResult.SuccessResponse(deviceId);
		} catch (Exception e) {
			LOG.error("------------闪贷获取设备号异常!! custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	@Log
	@Valid
	@RequestMapping("sd/getCreditAmt")
	public JsonResult sdGetCreditAmt(HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);			
			String  amt=afterCreditManager.sdGetCreditAmt(cust,Long.valueOf(3));			
			return JsonResult.SuccessResponse(amt);
		} catch (Exception e) {
			LOG.error("------------获取额度异常!! custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 上传签名图片
	 * @author zhaoqi01
	 * @param productId
	 * @param param
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/uploadSignature")
	@Valid
	public JsonResult<Object> uploadSignature(@PathVariable("productId") Long productId,
			@RequestBody SignatureParam param, HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			
			afterCreditManager.uploadSignature(cust,productId,param.getSignatureImg());
			return JsonResult.SuccessResponse(null);
		} catch (Exception e) {
			LOG.error("------------申请放款上传签名图片异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 查询授信后jumpUrl
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/queryJump")
	public JsonResult<Map<String,Object>> queryJump(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			
			String jumpUrl  = afterCreditManager.queryJump(cust,productId);
			Map<String,Object> map = new HashMap<String, Object>();
			if(null != jumpUrl) {
				map.put("isJump", true);
				map.put("jumpUrl", jumpUrl);
			} else {
				map.put("isJump", false);
			}
			return new JsonResult<Map<String,Object>>(map);
		} catch (Exception e) {
			LOG.error("------------查询授信后跳转页面异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 放贷首页数据
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log 
	@RequestMapping("{productId}/loanOrderIndex")
	public JsonResult<LoanOrderIndexDto> loanOrderIndex(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			
			LoanOrderIndexDto result = afterCreditManager.loanOrderIndex(productId,cust);
			return new JsonResult<LoanOrderIndexDto>(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现首页异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 判断可交单
	 * @author zhaoqi01
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/checkUser")
	public JsonResult<CheckOrderResultDto> checkUser(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			
			CheckOrderResultDto result = afterCreditManager.checkUser(productId,cust);
			return new JsonResult<CheckOrderResultDto>(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现可交单异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 绑卡，绑卡到信用猫
	 * @author zhaoqi01
	 * @param productId
	 * @param param
	 * @param request
	 * @return
	 */
	@Log
	@Valid
	@RequestMapping("{productId}/bindCard")
	public JsonResult<String> bindCard(@PathVariable("productId") Long productId,
			@RequestBody BindBankCardParamDto param, HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			afterCreditManager.bindCard(productId,param,cust);
			return JsonResult.SuccessResponse(null);
		} catch (Exception e) {
			LOG.error("------------贷款提现绑卡异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(e.getMessage());
		}
	}
	
	/**
	 * 查询已绑卡List
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/loanbindCardList")
	public JsonResult<LoanBindCardListDto> loanbindCardList(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			LoanBindCardListDto result = afterCreditManager.loanbindCardList(productId,cust);
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现查询绑卡列表异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(e.getMessage());
		}
	}
	
	/**
	 * 根据银行卡号查询银行信息
	 * @author zhaoqi01
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("queryBankByCard")
	public JsonResult<QueryBankByCardResultDto> queryBankByCard(@RequestBody Map<String,String> param, 
			HttpServletRequest request) {
		Cust cust = new Cust();
		String bankCard = null;
		try {
			cust = getCust(request);
			bankCard = param.get("bankCard");
			QueryBankByCardResultDto result = afterCreditManager.queryBankByCard(bankCard);
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现根据卡号查询银行信息异常!! custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(e.getMessage());
		}
	}
	
	/**
	 * 查询所有银行信息
	 * @author zhaoqi01
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("bankInfoList")
	public JsonResult<LoanBindCardListDto> bankInfoList(HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			LoanBindCardListDto result = afterCreditManager.bankInfoList();
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现查询银行列表异常!! custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(e.getMessage());
		}
	}
	
	/**
	 * 向第三方产品提交绑卡
	 * @author zhaoqi01
	 * @param productId
	 * @param param
	 * @param request
	 * @return
	 */
	@Log
	@Valid
	@RequestMapping("{productId}/submitBindCard")
	public JsonResult<Map<String,Boolean>> submitBindCard(@PathVariable("productId") Long productId,
			@RequestBody SubmitBindCardParamDto param, HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			Boolean  vercodeFlag = afterCreditManager.submitBindCard(productId,param,cust);
			Map<String,Boolean> map = new HashMap<String,Boolean>();
			map.put("vercodeFlag", vercodeFlag);
			return JsonResult.SuccessResponse(map);
		} catch (Exception e) {
			LOG.error("------------贷款提现提交绑卡异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(e.getMessage());
		}
	}
	
	/**
	 * 试算
	 * @author zhaoqi01
	 * @param request
	 * @return
	 */
	@Log
	@Valid
	@RequestMapping("{productId}/tryCalculate")
	public JsonResult<TryCalculateResultDto> tryCalculate(@PathVariable("productId") Long productId,
			@RequestBody TryCalculateParamDto param,HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			TryCalculateResultDto result = afterCreditManager.tryCalculate(productId,param,cust);
			return new JsonResult<TryCalculateResultDto>(result);
		} catch (Exception e) {
			LOG.error("------------试算异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
	}
	
	/**
	 * 提交订单
	 * @author zhaoqi01
	 * @param productId
	 * @param param
	 * @param request
	 * @return
	 */
	@Log
	@Valid
	@RequestMapping("{productId}/submitLoan")
	public JsonResult<String> submitLoan(@PathVariable("productId") Long productId,
			@RequestBody CreateOrderParamDto param,HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			String orderId = afterCreditManager.submitLoan(productId, param, cust);
			if(null == orderId) {
				return JsonResult.ErrorResponse(ERROR_MSG);
			}
		} catch (Exception e) {
			LOG.error("------------贷款提现提交订单异常!! productId="+productId+", custId=" + cust.getCustId(),e);
			return JsonResult.ErrorResponse(ERROR_MSG);
		}
		return JsonResult.SuccessResponse(null);
	}
	
	/**
	 * 查询合同
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/queryContract")
	public JsonResult<QueryContractResultDto> queryContract(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			QueryContractResultDto result = afterCreditManager.queryContract(productId,cust);
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现查询合同异常!! productId="+productId+", custId=" + cust.getCustId(),e);
		}
		return JsonResult.ErrorResponse(ERROR_MSG);
	}
	
	/**
	 * 查询订单状态
	 * @author zhaoqi01
	 * @param productId
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("{productId}/queryOrderStatus")
	public JsonResult<QueryOrderStatusResultDto> queryOrderStatus(@PathVariable("productId") Long productId,
			HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			QueryOrderStatusResultDto result = afterCreditManager.queryOrderStatus(productId,cust);
			return JsonResult.SuccessResponse(result);
		} catch (Exception e) {
			LOG.error("------------贷款提现查询订单状态异常!! productId="+productId+", custId=" + cust.getCustId(),e);
		}
		return JsonResult.ErrorResponse(ERROR_MSG);
	}
	
	/**
	 * 查询用户信息
	 * @author zhaoqi01
	 * @param request
	 * @return
	 */
	@Log
	@RequestMapping("queryCustInfo")
	public JsonResult<Cust> queryCustInfo(HttpServletRequest request) {
		Cust cust = new Cust();
		try {
			cust = getCust(request);
			cust = afterCreditManager.queryCustInfo(cust);
			return JsonResult.SuccessResponse(cust);
		} catch (Exception e) {
			LOG.error("------------贷款提现查询用户信息异常!! custId=" + cust.getCustId(),e);
		}
		return JsonResult.ErrorResponse(ERROR_MSG);
	}
	
}
